﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace cs5643.rigidbody
{
    public struct Pnt2D
    {
        public double x;
        public double y;

        public Pnt2D(double x, double y)
        {
            this.x = x;
            this.y = y;
        }

        public Pnt2D(Pnt2D p)
        {
            this.x = p.x;
            this.y = p.y;
        }

        public double SquaredLength { get { return x * x + y * y; } }

        public double Length { get { return Math.Sqrt(SquaredLength); } }

        internal Pnt2D Sub(Pnt2D p)
        {
            x -= p.x;
            y -= p.y;
            return this;
        }

        internal Pnt2D Add(Pnt2D p)
        {
            x += p.x;
            y += p.y;
            return this;
        }

        internal Pnt2D Times(double d)
        {
            x *= d;
            y *= d;
            return this;
        }

        internal void Set(Pnt2D p)
        {
            this.x = p.x;
            this.y = p.y;
        }

        internal static Pnt2D Sub(Pnt2D p, Pnt2D c)
        {
            return new Pnt2D(p.x - c.x, p.y - c.y);
        }

        internal double Dot(Pnt2D p)
        {
            return x * p.x + y * p.y;
        }

        internal void Normalize()
        {
            double length = Length;
            x /= length;
            y /= length;
        }

        /**  Returns (ax*by-ay*bx).  */
        static double crossZ(Pnt2D a, Pnt2D b) { return (a.x * b.y - a.y * b.x); }

        internal void ScaleAdd(Pnt2D p, double d)
        {
            x += p.x * d;
            y += p.y * d;
        }
    }
}
